package util

import "github.com/shopspring/decimal"

var decimalUnit decimal.Decimal

func init() {
	decimalUnit = decimal.New(1, 2)
}

// 元 => int分
func FromYuanToCent(f float64) int {
	return int(decimal.NewFromFloat(f).Mul(decimalUnit).IntPart())
}

// 从decimal转化为float
func FromDecimalToFloat(f decimal.Decimal) float64 {
	res, _ := f.Float64()
	return res
}

// 分 => decimal元
func FromCentToYuan(i int) float64 {
	res, _ := decimal.NewFromInt(int64(i)).DivRound(decimalUnit, 2).Float64()
	return res
}
